package weixin

import (
	"log"
)

func (c *Weixin_Class) CheckRmoteIP(ip string) bool {
	if c.tx_ip_map == nil {
		c.mAccessToken = c.getAccessToken(true)
		data, err := c.httpGetJson("https://qyapi.weixin.qq.com/cgi-bin/getcallbackip?access_token=" + c.mAccessToken)
		if err != nil {
			log.Println("getcallbackip error:", err)
			return false
		}
		errcode := data["errcode"].(float64)
		if errcode != 0 {
			log.Println("getcallbackip error:", data["errmsg"].(string))
			return false
		}

		c.tx_ip_map = make(map[string]int)
		ips := data["ip_list"].([]any)
		for _, v := range ips {
			c.tx_ip_map[v.(string)] = 1
		}
	}

	if _, ok := c.tx_ip_map[ip]; ok {
		return true
	}

	return false
}
